package com.isoft.controller.user;
import com.isoft.bean.ResponseData;
import com.isoft.bean.ResultCode;
import com.isoft.controller.BaseController;
import com.isoft.entity.user.User;
import com.isoft.entity.vo.LoginParam;
import com.isoft.service.user.UserService;
import com.isoft.service.email.CommonService;
import com.isoft.service.email.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LiuGaoMing
 * @version 1.0
 * @description:用户控制层
 * @date 2024/6/3 11:49
 */
@RestController
@RequestMapping
@CrossOrigin
public class UserController extends BaseController {

    @Autowired
    private UserService userService;
    @Autowired
    private CommonService commonService;
    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseData<Object> login(@RequestBody User user){
        user = userService.Login(user.getUsername(), user.getUserpass());
        if (user!=null){
            user.setUserpass( null );
        }
        return returnObject( user );
    }
    /**
     * vip用户登录
     * @param user
     * @return
     */
    @PostMapping("/vipLogin")
    public ResponseData<Object> vipLogin(@RequestBody User user) {
        User loggedUser = userService.vipLogin(user.getUsername(), user.getUserpass());
        if (loggedUser != null && loggedUser.getValid() == 0) {
            return returnObject(null, "3333", "null");
        }
        if (loggedUser != null) {
            loggedUser.setUserpass(null);
        }
        return returnObject(loggedUser);
    }
    /**
     * 普通用户登录
     * @param user
     * @return
     */
    @PostMapping("/commonLogin")
    public ResponseData<Object> commonLogin(@RequestBody User user){
        User loggedUser = userService.commonLogin(user.getUsername(), user.getUserpass());
        if (loggedUser != null && loggedUser.getValid() == 0) {
            return returnObject(null, "3333", "null");
        }
        if (loggedUser != null) {
            loggedUser.setUserpass(null);
        }
        return returnObject(loggedUser);
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/register")
    public ResponseData<Boolean> registerUser(@RequestBody User user) {
        return returnBoolean( userService.add(user) );
    }
    /**
     * 修改密码
     */
    @PostMapping("/update")
    public ResponseData<Boolean> updateUser(@RequestBody LoginParam loginParam) {
        return returnBoolean( userService.update(loginParam) );
    }

    /**
     * 用于获取邮箱权限码请求的接口
     * @param emailJson
     * @return
     */
    @PostMapping("/code/request")
    public R getRequestPermissionCode(@RequestBody String emailJson) {
        return commonService.getRequestPermissionCode(emailJson);
    }

    /**
     * 邮箱验证码接口
     * @param loginParam
     * @return
     */
    @PostMapping("/code/email")
    public R sendEmailCode(@RequestBody LoginParam loginParam) {
        return commonService.sendEmailCode(loginParam);
    }


    //    分页查询
    @GetMapping("/userInfo/page")
    public ResponseData<Object> getUserInfosPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "5") int pageSize) {
        try {
            List<User> users = userService.findUserInfoByPage(currentPage, pageSize);
            long totalElements = userService.selectUserInfoCount();
            return ResponseData.success(users, totalElements);
        } catch (Exception e) {
            // 处理异常，返回错误信息
            return ResponseData.failure(String.valueOf(ResultCode.COMMON_FAIL.getCode()), "获取数据失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    public ResponseData<Boolean> deleteUser(@PathVariable Integer id) {
        return returnBoolean( userService.delete(id));
    }

    @GetMapping("/select")
    public ResponseData<Object> selectUserByUsername(String username) {
        List<User> user = userService.selectUserByUsername(username);
        return returnObject( user );
    }
@PutMapping("/updateInfo")
    public ResponseData<Boolean> updateUserInfo(@RequestBody User user) {
        Boolean updateUserInfo = userService.updateUserInfo(user);
        return returnBoolean(updateUserInfo);
    }
    @GetMapping("/selectAuthority/{userid}")
    public ResponseEntity<Integer> getAuthorityById(@PathVariable Integer userid) {
        int authorityId = userService.selectAuthority(userid);
        return ResponseEntity.ok(authorityId);
    }
}
